<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author Cheng
-->
<template>
  <BasicDrawer
    :okAuth="'unitmanage:unitBuilding:edit'"
    @register="registerDrawerFloor"
    width="60%"
  >
    <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'unitmanage:unitFloor:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>

      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.floorNum }}
        </a>
      </template>
      <template #picPath="{ record }">
        <Image style="height: 100px;" v-if="record.picPath" :src="record.picPath" width="100" height="100" alt="" />
        <div style="height: 100px;text-align: center;line-height: 100px;color:#32CD32" v-else>
         <img src="/@/assets/images/noData.png" alt="">
        </div>
      </template>
    </BasicTable>
    <InputForm  :buildingId="tidValue" @register="registerModal" @success="handleSuccess" />
  </div>
  </BasicDrawer>
</template>
<script lang="ts" setup name="ViewsUnitmanageUnitFloorList">
  import { unref, defineProps,watch,onMounted,ref,nextTick } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { BasicDrawer, useDrawerInner, useDrawer } from '/@/components/Drawer';
  import { unitFloorDelete, unitFloorListData } from '/@/api/unitmanage/unitFloor';
  import { useModal } from '/@/components/Modal';
  import { FormProps } from '/@/components/Form';
  import {Image} from 'ant-design-vue'
  import InputForm from './form.vue';
  
  const { t } = useI18n('unitmanage.unitFloor');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  // 定义 props 接收 tid
const props = defineProps<{
  tid: Number; // 根据 tid 的实际类型调整
}>();



  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('楼层管理表管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { md: 8, lg: 6 },
    labelWidth: 90,
    schemas: [
      {
        label: t('楼层数'),
        field: 'floorNum',
        component: 'Input',
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('楼层数'),
      dataIndex: 'floorNum',
      key: 'a.floor_num',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('楼层图片'),
      dataIndex: 'picPath',
      key: 'a.pic_path',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'picPath',
    },
  ];
  const [registerDrawerFloor, { setDrawerProps, closeDrawer }] = useDrawerInner();


  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑楼层管理表'),
        onClick: handleForm.bind(this, { tid: record.tid }),
        auth: 'unitmanage:unitFloor:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除楼层管理表'),
        popConfirm: {
          title: t('是否确认删除楼层管理表'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'unitmanage:unitFloor:edit',
      },
    ],
  };

  const [registerTable, { reload }] = useTable({
    api: unitFloorListData,
    beforeFetch: (params) => {
      params={...params, buildingId: props.tid}
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerModal, { openModal }] = useModal();

  function handleForm(record: Recordable) {
    openModal(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { tid: record.tid };
    const res = await unitFloorDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  // 定义一个响应式变量来存储buildingId
const tidValue = ref(props.tid);
// 监听props.buildingId的变化
watch(() => props.tid, (newValue) => {
  tidValue.value = newValue;
  console.log('tid变化了,新值为:', newValue);
  if (newValue!== null) {
    nextTick(() => {
        reload();
      });
  }
});
  onMounted(() => {
  console.log('组件挂载时tid的值为:',  tidValue.value);
  if (tidValue.value!== null) {
    reload();
  }
});

</script>
